// pages/index/index.js
const API = require('../../utils/api.js'); // 引入封装好的 API 方法
Page({
  data: {
    activeTab: 'current', // current | history
    queryParam:{},
    searchText: '',
    showModal: false,//二维码弹出框
    filteredList: [],
    pageNum: 1, // 当前页码
    pageSize: 5, // 每页显示数量
    hasMore: false, // 是否还有更多数据
    base64:'',
    triggered:false
  },
  onLoad() {
  },
  onRefresh(){
    this.setData({
      pageNum:1,
      hasMore:false
    })
    this.getList()
    this.setData({
      triggered:false
    })
  },
  async onReachBottom(){
    if (!this.data.hasMore){
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      });
      return;
    }
    let tempList = this.data.filteredList;
    this.setData({
      pageNum:this.data.pageNum+1,
    })
   await this.getList()
    this.setData({
      filteredList:[...tempList,...this.data.filteredList]
    })
  },
  // 切换Tab
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab,
      searchText: ''
    })
    this.getList();
  },

  // 处理搜索
  handleSearch(e) {
    this.setData({
      searchText: e.detail.value.trim()
    })
    this.getList();
  },
  clearSearch() {
    this.setData({ searchText: '' })
    this.getList();
  },
  // 扫一扫
  handleScan() {
    wx.scanCode({
      success: (res) => {
        wx.showToast({
          title: '扫码成功',
          icon: 'success'
        });
        let jsonCode = JSON.parse(res.result)
        console.log('扫码结果:', jsonCode);
        wx.navigateTo({
          url: jsonCode.toPath,
        })
      },
      fail: (err) => {
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        });
        console.error('扫码失败:', err);
      }
    });
  },
  // 发布签到
  handleCreate() {
    wx.navigateTo({
      url: '/pages/sign/add/addSign'
    });
  },
  // 签到
  async handleSign(e) {
    const signDataRow = e.currentTarget.dataset.row;
    //普通签到
    if("00"===signDataRow.signType){
      await API.signIn(signDataRow.id)
      wx.showToast({
        title: '签到成功',
        icon: 'success'
      });
      this.getList()
    }else if("01"===signDataRow.signType){
      //扫码签到
      this.handleScan();
    }else{
      //位置签到
      wx.navigateTo({
        url: '/pages/sign/locationSign/locationSign?item='+JSON.stringify(signDataRow),
      })
    }
  },
  onShow(){
    this.getList();
  },
  //查询数据列表
  async getList(){
    wx.showLoading({
      title: '搜索中...',
      mask: true
    });
    this.setData({
      'queryParam.signStatus':this.data.activeTab=='current'?'1':'2',
      'queryParam.signTitle': this.data.searchText
    })
    let res = await API.signDataList({...this.data.queryParam,pageSize:this.data.pageSize,pageNum:this.data.pageNum})
    this.setData({ 
      filteredList: res.records, 
      hasMore:res.pages>this.data.pageNum?true:false,
    });
    wx.hideLoading()
  },
  //展示二维码
 async showQrCode(e){
    let id = e.currentTarget.dataset.id
    this.setData({
      showModal:true
    })
   let base64Res = await API.jsonQr(id)
   console.log('base64',base64Res)
   this.setData({
     base64:base64Res
   })
  },
  // 隐藏弹窗
  hideModal() {
    this.setData({ 
      showModal: false ,
      base64:''
    });
  },
  // 阻止弹窗内容点击事件冒泡
  stopPropagation() {},
});